package com.xbsg.protocol.xf.server.netty;

import com.xbsg.protocol.xf.entiy.AppDataUtil;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class DataUtil {
    public static String bytes2String(byte... bytes){
        StringBuilder stringBuilder = new StringBuilder();
        for(byte b :bytes){
            stringBuilder.append(byteToInt(b));
        }
        return stringBuilder.toString();
    }

    //byte转10进制
    public static int byteToInt(byte res) {
        return res & 0xff;
    }

    //解析 日期标签
    public static String parse_time_stamp(byte[] bytes,int start,int length){
        StringBuilder sb = new StringBuilder();
        for(int i=start + length-1;i >= start;i--){
            String str = bytes2String(bytes[i]);
            if(str.length() == 1){
                sb.append("0");
            }
            sb.append(bytes2String(bytes[i]));
            sb.append(" ");
        }
        return AppDataUtil.parse_time_stamp(sb.toString());
    }

    //解析部件说明
    public static String parse_part_desc(byte[] bytes,int start,int length){
        //字节数组转10进制数组
        byte[] byteTemp = new byte[length];
        System.arraycopy(bytes,start,byteTemp,0,length);

        ByteBuffer byteBuffer = ByteBuffer.wrap(byteTemp);
        CharBuffer gb18030 = Charset.forName("GB18030").decode(byteBuffer);
        ByteBuffer utf8 = Charset.forName("UTF8").encode(gb18030);

        String result = new String(utf8.array());
        return result.replace("\u0000", "");
    }

    //解析asii
    public static String get_part_address(byte[] bytes,int start,int length){
        StringBuilder sb = new StringBuilder();
        for(int i =start;i<start + length;i++){
            int value = byteToInt(bytes[i]);
            sb.append((char) value);
        }
        return sb.toString();
    }

    //数组合并

    /**
     * ascii码
     * @param b
     * @return
     */
    public static String byteToAscii(byte b) {
        StringBuilder sb = new StringBuilder();
        int value = byteToInt(b);
        sb.append((char) value);
        return sb.toString();
    }



}
